package com.example.fragment_1;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import android.os.Bundle;
import android.util.SparseArray;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements ContentFragment.OnItemSelectedListener,LoginDialog.OnLoginInputListener{


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initFragment();
        replaceFragment(R.id.fragment_title,fragments.get(R.id.fragment_title));
        replaceFragment(R.id.fragment_content,fragments.get(R.id.fragment_content));
    }

    private SparseArray<Fragment> fragments;
    private void initFragment() {
        fragments = new SparseArray();
        fragments.put(R.id.fragment_title,TitleFragment.newInstance());
        fragments.put(R.id.fragment_content,ContentFragment.newInstance("内容fragment"));
    }

    private void replaceFragment(int containerId, Fragment fragment){
        getSupportFragmentManager()
                .beginTransaction()
                .replace(containerId,fragment)
                .addToBackStack(null)
                .commit();
    }

    @Override
    public void OnItemSelected(String content) {
        Toast.makeText(this,content,Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.mymenu,menu);

        return true;


    }
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == R.id.item1) {
            //final LoginDialog  loginDialog = new
        }
    return true;
    }


    @Override
    public void onDialogPositiveClick(String username, String password) {
        Toast.makeText(this,username+"password",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDialogNegativeClick(DialogFragment dialog) {
        Toast.makeText(this,"取消",Toast.LENGTH_SHORT).show();
    }
}